package com.yuyou.fn.tms.vo;

import com.alibaba.fastjson.JSONArray;
import com.yuyou.fn.educational.entity.StudentReg;
import com.yuyou.fn.educational.vo.PayLecture;
import com.yuyou.fn.settlement.entity.NewPayRecord;
import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty;
import org.apache.commons.lang3.StringUtils;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

@ApiModel("报名")
public class TmsReg {

    @ApiModelProperty("报名id")
    private Long regId ;//报名id

    @ApiModelProperty("学生ID")
    private Long regStudentId ;//学生ID

    @ApiModelProperty("班级Id")
    private Long regClassId ;//班级Id

    @ApiModelProperty("报名状态（0：正常，1：已撤单，2：已退班，4：已转班 )")
    private int regState ;//报名状态（0：正常，1：已撤单，2：已退班，4：已转班 )

    @ApiModelProperty("学生名字")
    private String stuName ;//学生名字

    @ApiModelProperty("是否已退费（0-否1-是）")
    private int regIsReturn ;//是否已退费（0-否1-是）

    @ApiModelProperty("是否申请退费 0未申请 1已申请")
    private int regIsApplyRefund ;//是否申请退费 0未申请 1已申请

    @ApiModelProperty("报名方式 （0:前台报名，1：网报 ）")
    private int regStyle ;//报名方式 （0:前台报名，1：网报 ）

    @ApiModelProperty("缴费状态 0未交费1已缴费2欠费")
    private int regPayed ;//缴费状态 0未交费1已缴费2欠费

    @ApiModelProperty("报班起始课次")
    private int regStartNum ;//报班起始课次

    @ApiModelProperty("报班结束课次")
    private int regEndNum ;//报班结束课次

    @ApiModelProperty("原价（总金额）")
    private BigDecimal regPrice ;//原价（总金额）

    @ApiModelProperty("已缴")
    private BigDecimal regPayPrice ;//已缴

    @ApiModelProperty("优惠总额")
    private BigDecimal regDiscount ;//优惠总额

    @ApiModelProperty("退费金额")
    private BigDecimal regRefund ;//退费金额

    @ApiModelProperty("创建时间")
    private Date regCreateDate ;//创建时间

    @ApiModelProperty("修改时间")
    private Date regModifyDate ;//修改时间

    @ApiModelProperty("报名创建人名")
    private String regCreatorName ;//报名创建人名

    @ApiModelProperty("报名创建人Id")
    private Long regCreatorId ;//报名创建人Id

    @ApiModelProperty("订单ID")
    private Long regOrderId ;//订单ID

    @ApiModelProperty("已缴讲次数（按讲次缴费）")
    private int regPayedCurrNum ;//已缴讲次数（按讲次缴费）

    private TmsRegOrder regOrder ;

    private List<TmsRegDiscount> regDiscounts ;

    private List<TmsRegDetail> regDetails ;

    public TmsReg(StudentReg studentReg,TmsRegOrder order , List<NewPayRecord> payRecords) {

        this.setRegId(studentReg.getRegId());
        this.setRegStudentId(studentReg.getStudentId());
        this.setRegClassId(studentReg.getClassId());
        //报名状态（0：正常，1：已撤单，2：已退班，4：已转班 )
        //0：在读 1：转班 2：退账户 3：退班退费 4:无效
        if(studentReg.getRegStatus() == 0 ){
            this.setRegState(0);
        }if(studentReg.getRegStatus() == 1 ){
            this.setRegState(4);
        }if(studentReg.getRegStatus() == 2 ){
            this.setRegState(2);
        }if(studentReg.getRegStatus() == 3 ){
            this.setRegState(2);
        }if(studentReg.getRegStatus() == 4 ){
            this.setRegState(1);
        }

        this.setStuName(studentReg.getStudentName());
        this.setRegIsReturn(studentReg.getRefundAmount() != null && studentReg.getRefundAmount().doubleValue() > 0 ?  1 : 0);
        this.setRegIsApplyRefund(studentReg.getRefundStatus() != null && studentReg.getRegStatus() == 1 ? 1 : 0);
        //0：线下 1：线上 2:转班 3:tms
        this.setRegStyle(studentReg.getRegFrom() == 0 || studentReg.getRegFrom() == 2 ? 0 : 1 );//报名方式 （0:前台报名，1：网报 ）
        if (studentReg.getChargingStatus()==1){
            this.setRegPayed(2);
        }else if (studentReg.getChargingStatus()==2){
            this.setRegPayed(1);
        }else {
            this.setRegPayed(studentReg.getChargingStatus());
        }

        this.setRegStartNum(studentReg.getInitStartAmount());
        this.setRegEndNum(studentReg.getInitEndAmount());
        this.setRegPrice(studentReg.getSrcTotalAmount());
        this.setRegPayPrice(studentReg.getPayAmount());
        this.setRegDiscount(studentReg.getSrcTotalAmount().subtract( studentReg.getTotalAmount() ));
        this.setRegRefund(studentReg.getRefundAmount());
        this.setRegCreateDate(studentReg.getCreateTime());
        this.setRegModifyDate(studentReg.getUpdateTime());
        this.setRegCreatorName(studentReg.getOperator());
        this.setRegCreatorId(studentReg.getOperatorId());
        this.setRegOrderId(studentReg.getCourseOrderId());
        this.setRegPayedCurrNum(studentReg.getPayLectureAmount());
        if(StringUtils.isNotEmpty(studentReg.getDiscountDetail())){
            //[网报]在读学员续读尊享优惠（连报3科）500元/科|500|984730794419326976,[网报]每科减20优惠|20|984734640981938176

            List<TmsRegDiscount> discounts = new ArrayList<>(4 );
            for(String str : studentReg.getDiscountDetail().split(",") ){
                String[] items = str.split("\\|");
                TmsRegDiscount tmsRegDiscount = new TmsRegDiscount();
                tmsRegDiscount.setrDisDisId(Long.valueOf(items[2]));
                tmsRegDiscount.setrDisRegId(studentReg.getRegId());
                tmsRegDiscount.setrDisMoney(new BigDecimal(items[1]));
                tmsRegDiscount.setrDisName(items[0]);
                tmsRegDiscount.setrDisStyle(1);
                tmsRegDiscount.setrDisVal(tmsRegDiscount.getrDisMoney());
                discounts.add(tmsRegDiscount);
            }
            this.setRegDiscounts(discounts);
        }

        if(payRecords!= null && !payRecords.isEmpty() ){
            List<TmsRegDetail> regDetails = new ArrayList<>(payRecords.size());
            for(NewPayRecord payRecord : payRecords ){
                if(StringUtils.isEmpty(payRecord.getExtra())){
                    continue;
                }
                List<PayLecture> payLectures = JSONArray.parseArray(payRecord.getExtra()).toJavaList(PayLecture.class);
                for(PayLecture payLecture : payLectures ){
                    if(payLecture.getRegId().equals( studentReg.getRegId() )){
                        TmsRegDetail tmsRegDetail = new TmsRegDetail();
                        tmsRegDetail.setTrdRegId(payLecture.getRegId());
                        tmsRegDetail.setTrdMoney(payLecture.getPayAmount());
                        tmsRegDetail.setTrdOperationDate(payRecord.getCreateTime());
                        tmsRegDetail.setTrdFinaId(payRecord.getPayRecordId());
                        tmsRegDetail.setTrdType(payRecord.getFirstPay() == 1 ? 1 : 2 );
                        tmsRegDetail.setTrdCurrStartNum(payLecture.getStart());
                        tmsRegDetail.setTrdCurrEndNum(payLecture.getEnd());
                        regDetails.add(tmsRegDetail);
                    }
                }
            }
            this.setRegDetails(regDetails);
        }
        this.setRegOrder(order);


    }

    public Long getRegId() {
        return regId;
    }

    public void setRegId(Long regId) {
        this.regId = regId;
    }

    public Long getRegStudentId() {
        return regStudentId;
    }

    public void setRegStudentId(Long regStudentId) {
        this.regStudentId = regStudentId;
    }

    public Long getRegClassId() {
        return regClassId;
    }

    public void setRegClassId(Long regClassId) {
        this.regClassId = regClassId;
    }

    public int getRegState() {
        return regState;
    }

    public void setRegState(int regState) {
        this.regState = regState;
    }

    public String getStuName() {
        return stuName;
    }

    public void setStuName(String stuName) {
        this.stuName = stuName;
    }

    public int getRegIsReturn() {
        return regIsReturn;
    }

    public void setRegIsReturn(int regIsReturn) {
        this.regIsReturn = regIsReturn;
    }

    public int getRegIsApplyRefund() {
        return regIsApplyRefund;
    }

    public void setRegIsApplyRefund(int regIsApplyRefund) {
        this.regIsApplyRefund = regIsApplyRefund;
    }

    public int getRegStyle() {
        return regStyle;
    }

    public void setRegStyle(int regStyle) {
        this.regStyle = regStyle;
    }

    public int getRegPayed() {
        return regPayed;
    }

    public void setRegPayed(int regPayed) {
        this.regPayed = regPayed;
    }

    public int getRegStartNum() {
        return regStartNum;
    }

    public void setRegStartNum(int regStartNum) {
        this.regStartNum = regStartNum;
    }

    public int getRegEndNum() {
        return regEndNum;
    }

    public void setRegEndNum(int regEndNum) {
        this.regEndNum = regEndNum;
    }

    public BigDecimal getRegPrice() {
        return regPrice;
    }

    public void setRegPrice(BigDecimal regPrice) {
        this.regPrice = regPrice;
    }

    public BigDecimal getRegPayPrice() {
        return regPayPrice;
    }

    public void setRegPayPrice(BigDecimal regPayPrice) {
        this.regPayPrice = regPayPrice;
    }

    public BigDecimal getRegDiscount() {
        return regDiscount;
    }

    public void setRegDiscount(BigDecimal regDiscount) {
        this.regDiscount = regDiscount;
    }

    public BigDecimal getRegRefund() {
        return regRefund;
    }

    public void setRegRefund(BigDecimal regRefund) {
        this.regRefund = regRefund;
    }

    public Date getRegCreateDate() {
        return regCreateDate;
    }

    public void setRegCreateDate(Date regCreateDate) {
        this.regCreateDate = regCreateDate;
    }

    public Date getRegModifyDate() {
        return regModifyDate;
    }

    public void setRegModifyDate(Date regModifyDate) {
        this.regModifyDate = regModifyDate;
    }

    public String getRegCreatorName() {
        return regCreatorName;
    }

    public void setRegCreatorName(String regCreatorName) {
        this.regCreatorName = regCreatorName;
    }

    public Long getRegCreatorId() {
        return regCreatorId;
    }

    public void setRegCreatorId(Long regCreatorId) {
        this.regCreatorId = regCreatorId;
    }

    public Long getRegOrderId() {
        return regOrderId;
    }

    public void setRegOrderId(Long regOrderId) {
        this.regOrderId = regOrderId;
    }

    public int getRegPayedCurrNum() {
        return regPayedCurrNum;
    }

    public void setRegPayedCurrNum(int regPayedCurrNum) {
        this.regPayedCurrNum = regPayedCurrNum;
    }

    public List<TmsRegDiscount> getRegDiscounts() {
        return regDiscounts;
    }

    public void setRegDiscounts(List<TmsRegDiscount> regDiscounts) {
        this.regDiscounts = regDiscounts;
    }

    public TmsRegOrder getRegOrder() {
        return regOrder;
    }

    public void setRegOrder(TmsRegOrder regOrder) {
        this.regOrder = regOrder;
    }

    public List<TmsRegDetail> getRegDetails() {
        return regDetails;
    }

    public void setRegDetails(List<TmsRegDetail> regDetails) {
        this.regDetails = regDetails;
    }
}
